﻿using MichMan.Utilities.Requests;
using Microsoft.Silverlight.Testing;
using Microsoft.Silverlight.Testing.UnitTesting.Metadata.VisualStudio;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using System.Xml.Linq;
using System;
using System.Linq;
using MichMan.Samples;
using System.Collections.Generic;

namespace MichMan.Utilities.UnitTest
{
    public class Label
    {
        public string Caption { get; set; }
    }

    [TestClass]
    public class RequestTests : WorkItemTest 
    {
        Label Label1 = new Label();
        [TestMethod]
        [Asynchronous]
        public void BunchOfSamples()
        {
            // XElement
            DataRequest<XElement> albumSearch = new DataRequest<XElement>();
            albumSearch.Uri = new Uri(@"http://catalog.zune.net/v3.0/music/album?q=Thriller");
            albumSearch.RunRequestCompleted += (s, e) => { Label1.Caption = albumSearch.Response.GetZPathValue("entry[1]/title"); };
            TestRequest(albumSearch);
        }

        [TestMethod]
        [Asynchronous]
        public void BunchOfSamples2()
        {
            AtomFeedRequest<AlbumSummary> albumSearch = new AtomFeedRequest<AlbumSummary>();
            albumSearch.Uri = new Uri(@"http://catalog.zune.net/v3.0/music/album?q=Thriller");
            albumSearch.RunRequestCompleted += (s, e) => { Label1.Caption = albumSearch.Response.Entries[0].Title; };
            TestRequest(albumSearch);
        }

        [TestMethod]
        [Asynchronous]
        public void BunchOfSamples3()
        {
            SyndicationRequest albumSearch = new SyndicationRequest();
            albumSearch.Uri = new Uri(@"http://catalog.zune.net/v3.0/music/album?q=Thriller");
            albumSearch.RunRequestCompleted += (s, e) => { Label1.Caption = albumSearch.Response.Items.ToList()[0].Title.Text; };
            TestRequest(albumSearch);
        }

        [TestMethod]
        [Asynchronous]
        public void BunchOfSamples4()
        {
            AlbumSearchRequest albumSearch = new AlbumSearchRequest() { Query = "Thriller" };
            albumSearch.RunRequestCompleted += (s, e) => { Label1.Caption = albumSearch.Response.Entries[0].Title; };
            TestRequest(albumSearch);
        }

        private void TestRequest<TResp>(IRequest<TResp> request)
        {
            request.RunRequestCompleted += (s, e) =>
            {
                Assert.IsFalse(request.Uri == null);
                Assert.IsFalse(request.Response == null);
                Assert.IsTrue(request.Error == null);
                Assert.IsTrue(request.RequestStatus == RequestStatus.Succeeded);

                TestComplete();
            };
            request.RunRequestAsync();
        }

        [TestMethod]
        [Asynchronous]
        public void SimpleXmlRequestTest()
        {
            TestRequest(new CatalogDataRequest());
        }

        [TestMethod]
        [Asynchronous]
        public void XPathRequestTest()
        {
            TestRequest(new TrackDetailsRequest());
        }

        [TestMethod]
        [Asynchronous]
        public void SimpleJsonRequestTest()
        {
            TestRequest(new YahooImageRequest());
        }

        [TestMethod]
        [Asynchronous]
        public void CompoundRequestTest()
        {
            CompoundCatalogRequest request = new CompoundCatalogRequest();
            request.RunRequestCompleted += (s, e) =>
            {
                Assert.IsFalse(request.Response == null);
                Assert.IsTrue(request.Error == null);
                Assert.IsTrue(request.RequestStatus == RequestStatus.Succeeded);

                TestComplete();
            };
            request.RunRequestAsync();
        }

        [TestMethod]
        [Asynchronous]
        public void RetryRequestTest()
        {
            TestRequest(new RetryRequest());
        }
        

        [TestMethod]
        [Asynchronous]
        public void CustomDataRequestTest()
        {
            TestRequest(new CustomDataRequest());
        }

        [TestMethod]
        [Asynchronous]
        public void UnsupportedCustomDataRequestTest()
        {
            UnsupportedCustomDataRequest request = new UnsupportedCustomDataRequest();
            request.RunRequestCompleted += (s, e) =>
            {
                Assert.IsFalse(request.Uri == null);
                Assert.IsTrue(request.Response == null);
                Assert.IsFalse(request.Error == null);
                Assert.IsTrue(request.RequestStatus == RequestStatus.Error);
                TestComplete();
            };
            request.RunRequestAsync();
        }

    }
}
